package org.itboys.fuzion.mysql.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import org.itboys.fuzion.mysql.dao.FuziongMemberStatisticsMapper;
import org.itboys.fuzion.mysql.orm.FuziongMemberStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;

/**
 * 大乱斗会员的重要信息的总记录
 * @author weisky
 *
 * 2016年4月19日
 */
@Service
public class FuziongMemberStatisticsService {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private FuziongMemberStatisticsMapper fuziongMemberStatisticsMapper;
	
	/**
	 * 删除一条记录
	 * @param id
	 */
	@Transactional
    public void delete(Long id){
    	FuziongMemberStatistics fms = getById(id);
    	fuziongMemberStatisticsMapper.delete(id);
    	logger.info("删除记录成功：FuziongMemberStatistics="+JSON.toJSONString(fms)+",删除时间="+new Date());
    }

    /**
     * 新增一条记录
     * @param record
     */
	@Transactional
    public boolean insert(FuziongMemberStatistics record){
    	try{
    		if(record == null || record.getMemberId() == null){
    			return false;
    		}
    		
    		if(record.getMemberId() != 0L){
    			FuziongMemberStatistics fmsList = fuziongMemberStatisticsMapper.getByMemberId(record.getMemberId());
        		if(fmsList != null){
        			logger.error("会员ID="+record.getMemberId()+"在数据库中已存在，不允许有相同的memberID同时存在,FuziongMemberStatistics="
    						+JSON.toJSONString(record)+",操作时间="+new Date());
        			return false;
        		}
    		}
    		
        	fuziongMemberStatisticsMapper.insert(record);
        	
        	logger.info("新增总记录成功：FuziongMemberStatistics="+JSON.toJSONString(record)+",新增时间="+new Date());
			return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("FuziongMemberStatisticsService.insert err", e);
    	}
    	return false;
    }

    public FuziongMemberStatistics getById(Long id){
    	return fuziongMemberStatisticsMapper.getById(id);
    }

    /**
     * 修改记录值
     * @param record
     * @return
     */
    @Transactional
    public boolean update(FuziongMemberStatistics record){
    	try{
        	fuziongMemberStatisticsMapper.update(record);
        	logger.info("修改记录值成功：FuziongMemberStatistics="+record+",修改时间="+new Date());
        	return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("FuziongMemberStatisticsService.update error", e);
    	}
    	return false;
    }
    
    /**
     * 根据会员ID 回去信息
     * @param memberId
     * @return
     */
    public FuziongMemberStatistics getByMember(Long memberId){
    	return fuziongMemberStatisticsMapper.getByMemberId(memberId);
    }
    
    /**
     * 通过memberId 修改统计值
     * @param record
     * @return
     */
    @Transactional
    public boolean updateByMemberId(FuziongMemberStatistics record){
    	try{
    		if(record.getId() != null){
    			fuziongMemberStatisticsMapper.update(record);
    		}else{
    			fuziongMemberStatisticsMapper.updateByMemberId(record);
    		}
        	logger.info("修改记录值成功：FuziongMemberStatistics="+record+",修改时间="+new Date());
        	return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("FuziongMemberStatisticsService.updateByMemberId error", e);
    	}
    	return false;
    }
    
    /**
     * 通过memberId 修改金额
     * @param record
     * @return
     */
    @Transactional
    public boolean updateNowTotalMoneyByMemberId(FuziongMemberStatistics record){
    	try{
    		FuziongMemberStatistics fms = getByMember(record.getMemberId());
    		record.setNowTotalMoney(fms.getNowTotalMoney() + record.getNowTotalMoney());//可提现金额
    		record.setWithdrawTotalMoney(fms.getWithdrawTotalMoney() + record.getWithdrawTotalMoney());//历史提现总金额
        	fuziongMemberStatisticsMapper.updateByMemberId(record);
        	logger.info("修改记录值成功：原始FuziongMemberStatistics="+fms+",修改后的FuziongMemberStatistics="+record+",修改时间="+new Date());
        	return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("FuziongMemberStatisticsService.updateByMemberId error", e);
    	}
    	return false;
    }
    
    /**
     * 通过memberId 修改可提现金额
     * @param record
     * @return
     */
    @Transactional
    public boolean updateDynamicIncomeByMemberId(FuziongMemberStatistics record){
    	try{
    		FuziongMemberStatistics fms = getByMember(record.getMemberId());
    		record.setDynamicIncome(fms.getNowTotalMoney() + record.getNowTotalMoney());//可提现金额
    		record.setWithdrawTotalMoney(fms.getWithdrawTotalMoney() + record.getWithdrawTotalMoney());//历史提现总金额
        	fuziongMemberStatisticsMapper.updateByMemberId(record);
        	logger.info("修改记录值成功：原始FuziongMemberStatistics="+fms+",修改后的FuziongMemberStatistics="+record+",修改时间="+new Date());
        	return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("FuziongMemberStatisticsService.updateByMemberId error", e);
    	}
    	return false;
    }
    
    /**
     * 获取店铺收益
     * @param sellerId
     * @return
     */
    public FuziongMemberStatistics getSellerIncome(Long sellerId){
    	return fuziongMemberStatisticsMapper.getSellerIncome(sellerId);
    }
    
    /**
     * 根据积分等级  获取这个积分等级下的会员列表
     * @return
     */
    public List<Long> getMemberIdListByScoreId(Long scoreId, Long sellerId){
    	return fuziongMemberStatisticsMapper.getMemberIdListByScoreId(scoreId, sellerId);
    }
    
    /**
     * 获取店铺的收入信息
     * @param sellerId
     * @return
     */
    public FuziongMemberStatistics getBySellerId(Long sellerId){
    	return fuziongMemberStatisticsMapper.getBySellerId(sellerId);
    }

    /**
	 * 统计店铺的提现总额
	 * @param sellerId
	 * @return
     */
	public Long getSellerWithdrawTotalMoney(Long sellerId){
		return fuziongMemberStatisticsMapper.getSellerWithdrawTotalMoney(sellerId);
	}

	/**
	 * 获取所有的店铺记录
	 * @return
	 */
	public List<FuziongMemberStatistics> getSellerStatistics(){
		return fuziongMemberStatisticsMapper.getSellerStatistics();
	}

	/**
	 * 根据param参数  统计人数
	 * @return
	 */
	public Integer getMemberBuyType(Long scoreId, Long sellerId){
		Map<String,Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("scoreId",scoreId);
		param.put("sellerId",sellerId);
		Integer num = fuziongMemberStatisticsMapper.getMemberBuyType(param);
		if(num == null){
			return 0;
		}else{
			return num;
		}
	}

	/**
	 * 修改各种收益
	 * @param fuziongMemberStatistics
	 */
	@Transactional
	public void updateStatistics(FuziongMemberStatistics fuziongMemberStatistics){
		try{
			fuziongMemberStatisticsMapper.updateStatistics(fuziongMemberStatistics);
			logger.info("修改会员收益信息成功，FuziongMemberStatistics=" + JSON.toJSONString(fuziongMemberStatistics));
		}catch (Exception e){
			e.printStackTrace();
			logger.error("FuziongMemberStatisticsService.updateStatistics error", e);
		}
	}
}
